﻿using System;
using System.Windows.Navigation;
using AddressManager.Business.ViewModels;

namespace AddressManager.Business.Views
{
    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home
    {
        readonly HomeViewModel _viewModel;

        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home()
        {
            InitializeComponent();

            _viewModel = new HomeViewModel();
            DataContext = _viewModel;

            _viewModel.GettingFinished += ViewModelGettingFinished;

            Title = ApplicationStrings.HomePageTitle;
        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            LoadingStoryBoard.Begin();
            _viewModel.GetContacts();
        }

        private void ViewModelGettingFinished(object sender, EventArgs e)
        {
            LoadingIndicator.Visibility = System.Windows.Visibility.Collapsed;
            ContactsPanel.Visibility = System.Windows.Visibility.Visible;
            LoadingStoryBoard.Stop();
        }
    }
}